//
//  NSJSONSerialization+Change.m
//  shenbian
//
//  Created by xizi_lixuemin on 15/3/16.
//  Copyright (c) 2015年 qianfan. All rights reserved.
//

#import "NSJSONSerialization+Change.h"

@implementation NSJSONSerialization (Change)

+ (NSString *)JSONStringFromDictionary:(NSDictionary *)dict
{
    NSError *jsonError = nil;

    if (!dict) {
        return nil;
    }

    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&jsonError];

    if (jsonError) {
        return nil;
    }

    NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
     
    return json;
}

/*!
 * @brief 把格式化的JSON格式的字符串转换成字典
 * @param jsonString JSON格式的字符串
 * @return 返回字典
 */
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    

    return dic;
}
@end
